package com.bootdo.fx.controller;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.fx.domain.UserInfoDO;
import com.bootdo.fx.service.UserInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.fx.domain.UserShopCartDO;
import com.bootdo.fx.service.UserShopCartService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * @author 袁飞龙
 * @email 1992lcg@163.com
 * @date 2017-11-30 17:12:20
 */
 
@Controller
public class UserShopCartController {
	@Autowired
	private UserShopCartService userShopCartService;

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private IdWorker idWorker;
	
	@GetMapping("/fx/userShopCart")
	@RequiresPermissions("fx:userShopCart:userShopCart")
	String UserShopCart(){
	    return "fx/userShopCart/userShopCart";
	}
	
	@ResponseBody
	@GetMapping("/fx/userShopCart/list")
	@RequiresPermissions("fx:userShopCart:userShopCart")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<UserShopCartDO> userShopCartList = userShopCartService.list(query);
		int total = userShopCartService.count(query);
		PageUtils pageUtils = new PageUtils(userShopCartList, total);
		return pageUtils;
	}
	
	@GetMapping("/fx/userShopCart/add")
	@RequiresPermissions("fx:userShopCart:add")
	String add(){
	    return "fx/userShopCart/add";
	}

	@GetMapping("/fx/userShopCart/edit/{cartId}")
	@RequiresPermissions("fx:userShopCart:edit")
	String edit(@PathVariable("cartId") Long cartId,Model model){
		UserShopCartDO userShopCart = userShopCartService.get(cartId);
		model.addAttribute("userShopCart", userShopCart);
	    return "fx/userShopCart/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/fx/userShopCart/save")
	@RequiresPermissions("fx:userShopCart:add")
	public R save( UserShopCartDO userShopCart){
		if(userShopCartService.save(userShopCart)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/fx/userShopCart/update")
	@RequiresPermissions("fx:userShopCart:edit")
	public R update( UserShopCartDO userShopCart){
		userShopCartService.update(userShopCart);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping("/fx/userShopCart/remove")
	@ResponseBody
	@RequiresPermissions("fx:userShopCart:remove")
	public R remove( Long cartId){
		if(userShopCartService.remove(cartId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/fx/userShopCart/batchRemove")
	@ResponseBody
	@RequiresPermissions("fx:userShopCart:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] cartIds){
		userShopCartService.batchRemove(cartIds);
		return R.ok();
	}



	/*
    * 添加商品到购物车
    * */
	@RequestMapping("/wx/userShopCart/addShopCart")
	@ResponseBody
	public JsonResult addShopCart(@RequestParam Map<String,Object> params, HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO userInfo = (UserInfoDO) request.getSession().getAttribute("userInfo");

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		UserShopCartDO userShopCart = new UserShopCartDO();
		userShopCart.setGoodsId(String.valueOf(params.get("goodsId")));
		userShopCart.setGoodsName(String.valueOf(params.get("goodsName")));
		userShopCart.setGoodsPicUrll(String.valueOf(params.get("goodsPicUrll")));
		userShopCart.setGoodsPrice(new BigDecimal(String.valueOf(params.get("goodsPrice"))));
		userShopCart.setCartQuantity(Integer.parseInt(String.valueOf(params.get("cartQuantity"))));
		userShopCart.setGoodsOriginPrice(new BigDecimal(String.valueOf(params.get("goodsOriginPrice"))));
		userShopCart.setUserId(String.valueOf(params.get("userId")));

		userShopCart.setCartId(idWorker.nextId());
		userShopCart.setCartReateTime(sdf.format(new Date()));
		int b = userShopCartService.save(userShopCart);
		if(b > 0){
			return ResultUtil.renderSuccess();
		}else{
			return ResultUtil.renderError();
		}
	}

	/*
    * 展示购物车
    * */
	@RequestMapping("/wx/userShopCart/listShopCart")
	@ResponseBody
	public JsonResult listShopCart(String userId,HttpServletRequest request, HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		//UserInfoDO userInfo = (UserInfoDO) request.getSession().getAttribute("userInfo");
		Map<String,Object> map = new HashMap<>();
		map.put("userId",userId);
		List<UserShopCartDO> lsit = userShopCartService.list(map);
		return ResultUtil.renderSuccess(lsit);
	}


	/**
	 * 删除
	 */
	@RequestMapping( "/wx/userShopCart/delete")
	@ResponseBody
	public JsonResult delete(String ids,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Long[] cartIds = new Long[ids.length()];
		String[] str = ids.split(",");
		for(int i=0;i<str.length;i++){
			cartIds[i] = Long.parseLong(str[i]);
		}
		int num = userShopCartService.batchRemove(cartIds);
		if(num > 0){
			return ResultUtil.renderSuccess();
		}
		else{
			return ResultUtil.renderError();
		}
	}


}
